﻿Imports System.Data.SqlClient
Imports System.Data

Partial Class myaccount_pr
    Inherits System.Web.UI.Page
#Region "Functions"
    Sub ImportPO(PRNumber As String, SqlSource As SqlDataSource)
        Session("PRNumber") = PRNumber
        If PRNumber <> "" Then
            'SearchFromNAV.Close()

            'Dim dvSql As DataView = DirectCast(SqlDataSource2.Select(DataSourceSelectArguments.Empty), DataView)
            Dim dvSql As DataView = DirectCast(SqlSource.Select(DataSourceSelectArguments.Empty), DataView)

            For Each drvSql As DataRowView In dvSql
                Dim SearchDuplicateSQL = New SqlConnection(ConfigurationManager.ConnectionStrings("Management_Info_SysConnectionString").ConnectionString)
                Dim SearchDuplicateCommand = New SqlCommand()
                SearchDuplicateCommand.CommandText = "SELECT * FROM [Management_Info_Sys].[dbo].[pr_rcpt] WHERE [po_description] = @No_ and extra1 = @company"
                SearchDuplicateCommand.Parameters.AddWithValue("No_", drvSql("No_").ToString)
                SearchDuplicateCommand.Parameters.AddWithValue("company", "KR")
                SearchDuplicateCommand.Connection = SearchDuplicateSQL
                SearchDuplicateSQL.Open()
                Dim record As Integer = 0
                record = SearchDuplicateCommand.ExecuteScalar()
                SearchDuplicateSQL.Close()
                If record <= 0 Then
                    'Import
                    Dim ImportSql = New SqlConnection(ConfigurationManager.ConnectionStrings("Management_Info_SysConnectionString").ConnectionString)
                    Dim ImportCommand = New SqlCommand()
                    ImportCommand.CommandText = "INSERT INTO [dbo].[pr_rcpt] ([po_description],[vendor],[costcenter],[extrended_desc],[purchaser],[amount],[order_date],[extra1],[extra2])  " & _
                        "VALUES (@po_description,@vendor,@costcenter,@extrended_desc,@purchaser,@amount,@order_date,@extra1,@extra2)"
                    ImportCommand.Parameters.AddWithValue("po_description", drvSql("No_").ToString)
                    ImportCommand.Parameters.AddWithValue("vendor", drvSql("column1").ToString)
                    ImportCommand.Parameters.AddWithValue("costcenter", drvSql("Shortcut_Dimension_1_Code").ToString)
                    ImportCommand.Parameters.AddWithValue("extrended_desc", drvSql("Extended_Description").ToString)
                    ImportCommand.Parameters.AddWithValue("purchaser", drvSql("Purchaser_Code").ToString)
                    ImportCommand.Parameters.AddWithValue("amount", "0")
                    ImportCommand.Parameters.AddWithValue("order_date", drvSql("Order_Date").ToString)
                    ImportCommand.Parameters.AddWithValue("extra1", "KR")
                    ImportCommand.Parameters.AddWithValue("extra2", "")
                    ImportCommand.Connection = ImportSql
                    ImportSql.Open()
                    ImportCommand.ExecuteNonQuery()
                    ImportSql.Close()
                End If
            Next


        End If

    End Sub

    Sub ImportPOItems(PONumber As String, SqlSource As SqlDataSource)
        Session("PONumber") = PONumber
        If PONumber <> "" Then
            'SearchFromNAV.Close()

            'Dim dvSql As DataView = DirectCast(SqlDataSource2.Select(DataSourceSelectArguments.Empty), DataView)
            Dim dvSql As DataView = DirectCast(SqlSource.Select(DataSourceSelectArguments.Empty), DataView)

            For Each drvSql As DataRowView In dvSql
                Dim SearchDuplicateSQL = New SqlConnection(ConfigurationManager.ConnectionStrings("Management_Info_SysConnectionString").ConnectionString)
                Dim SearchDuplicateCommand = New SqlCommand()
                SearchDuplicateCommand.CommandText = "SELECT * FROM [Management_Info_Sys].[dbo].[po_items] WHERE [No_] = @No_ and extra1 = @company and extra2 = @linenumber"
                SearchDuplicateCommand.Parameters.AddWithValue("No_", drvSql("Document_No_").ToString)
                SearchDuplicateCommand.Parameters.AddWithValue("company", "KR")
                SearchDuplicateCommand.Parameters.AddWithValue("linenumber", drvSql("Line_No_").ToString)
                SearchDuplicateCommand.Connection = SearchDuplicateSQL
                SearchDuplicateSQL.Open()
                Dim record As Integer = 0
                record = SearchDuplicateCommand.ExecuteScalar()
                SearchDuplicateSQL.Close()
                If record <= 0 Then
                    'Import
                    Dim ImportSql = New SqlConnection(ConfigurationManager.ConnectionStrings("Management_Info_SysConnectionString").ConnectionString)
                    Dim ImportCommand = New SqlCommand()

                    ImportCommand.CommandText = "INSERT INTO [dbo].[po_items] ([TimeStamp],[Type],[No_],[Description],[UnitOfMeas],[PricePU],[Amount],[extra1],[extra2],[Quantity])  " & _
                        "VALUES (@TimeStamp,@Type,@No_,@Description,@UnitOfMeas,@PricePU,@Amount,@extra1,@extra2,@Quantity)"
                    ImportCommand.Parameters.AddWithValue("TimeStamp", "")
                    ImportCommand.Parameters.AddWithValue("Type", drvSql("Type").ToString)
                    ImportCommand.Parameters.AddWithValue("No_", drvSql("Document_No_").ToString)
                    ImportCommand.Parameters.AddWithValue("Description", drvSql("Description").ToString)
                    ImportCommand.Parameters.AddWithValue("UnitOfMeas", drvSql("Unit_of_Measure").ToString)
                    ImportCommand.Parameters.AddWithValue("PricePU", drvSql("Direct_Unit_Cost").ToString)
                    ImportCommand.Parameters.AddWithValue("Amount", drvSql("Amount").ToString)
                    ImportCommand.Parameters.AddWithValue("extra1", "KR")
                    ImportCommand.Parameters.AddWithValue("extra2", drvSql("Line_No_").ToString)
                    ImportCommand.Parameters.AddWithValue("Quantity", drvSql("Quantity_").ToString)
                    ImportCommand.Connection = ImportSql
                    ImportSql.Open()
                    ImportCommand.ExecuteNonQuery()
                    ImportSql.Close()
                End If
            Next


        End If

    End Sub
    Function GetExistingPOFromNav() As ArrayList

        Return Nothing
    End Function

    Function GetExistingPOFromMIS() As ArrayList


        Return Nothing
    End Function

    Sub IncrementPO()



    End Sub
#End Region

    Protected Sub GridView2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView2.SelectedIndexChanged
        Response.Redirect("pr.aspx?item=" & GridView2.SelectedRow.Cells(2).Text)
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Find PO's import to Local DB
        If Request.QueryString("item") <> "" Then
            ImportPO(Request.QueryString("item"), SqlDataSource2)
            If Request.QueryString("po") <> "" Then
                ImportPOItems(Request.QueryString("item"), POSqlDataSource)
            End If
        End If


    End Sub

    
    Protected Sub GridView3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView3.SelectedIndexChanged
        Response.Redirect("pr.aspx?item=" & Request.QueryString("item") & "&po=" & GridView3.SelectedRow.Cells(2).Text)
        'Request.QueryString.Add("po", GridView3.SelectedRow.Cells(2).Text)

    End Sub

End Class
